<!DOCTYPE html>
<html>
<head>
    <title>PlayCanvas Point Cloud</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="icon" type="image/png" href="../playcanvas-favicon.png" />
    <script src="../../build/output/playcanvas.js"></script>
    <style>
        body { 
            margin: 0;
            overflow-y: hidden;
        }
    </style>
</head>

<body>
    <!-- The canvas element -->
    <canvas id="application-canvas"></canvas>

    <!-- shaders -->
    <script id="vshader" type="x-shader/x-vertex">
        // Attributes per vertex: position
        attribute vec4 aPosition;

        uniform mat4   matrix_viewProjection;
        uniform mat4   matrix_model;
        uniform mat4   matrix_view;

        // time
        uniform float uTime;

        // Color to fragment program
        varying vec4 outColor;

        void main(void)
        {
            // Transform the geometry
            mat4 modelView = matrix_view * matrix_model;
            mat4 modelViewProj = matrix_viewProjection * matrix_model;
            gl_Position = modelViewProj * aPosition;

            // vertex in world space
            vec4 vertexWorld = matrix_model * aPosition;

            // use sine way to generate intensity value based on time and also y-coordinate of model
            float intensity = abs(sin(0.6 * vertexWorld.y + uTime * 1.0));

            // intensity smoothly drops to zero for smaller values than 0.9
            intensity = smoothstep(0.9, 1.0, intensity);

            // point size depends on intensity
            gl_PointSize = 12.0 * intensity;

            // color mixes red and yellow based on intensity
            outColor = mix(vec4(1.0, 1.0, 0.0, 1.0), vec4(0.9, 0.0, 0.0, 1.0), intensity);
        }
    </script>

    <script id="fshader" type="x-shader/x-fragment">
        precision lowp float;
        varying vec4 outColor;

        void main(void)
        {
            // just output color supplied by vertex shader
            gl_FragColor = outColor;
        }
    </script>

    <!-- The script -->
    <script>
        var entity, light, camera;
        var shader, material, diffuseMap;
        var canvas = document.getElementById("application-canvas");

        // Create the application and start the update loop
        var app = new pc.Application(canvas);
        app.start();

        // Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
        app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
        app.setCanvasResolution(pc.RESOLUTION_AUTO);

        window.addEventListener("resize", function () {
            app.resizeCanvas(canvas.width, canvas.height);
        });

        // Create an Entity with a camera component
        camera = new pc.Entity();
        camera.addComponent("camera", {
            clearColor: new pc.Color(0.1, 0.1, 0.1)
        });
        camera.translate(0, 7, 24);

        // Add entity into scene hierarchy
        app.root.addChild(camera);

        var url = "../assets/models/statue/statue.json";
        app.assets.loadFromUrl(url, "model", function (err, asset) {
            // Create a new Entity
            entity = new pc.Entity();

            // Add a model component, with the new asset
            var modelComponent = entity.addComponent("model", {
                type: "asset",
                asset: asset
            });
            app.root.addChild(entity);

            // Create the shader definition and shader from the vertex and fragment shaders
            var shaderDefinition = {
                attributes: {
                    aPosition: pc.SEMANTIC_POSITION
                },
                vshader: document.getElementById("vshader").textContent,
                fshader: document.getElementById("fshader").textContent
            };
            shader = new pc.Shader(app.graphicsDevice, shaderDefinition);

            // Create a new material with the new shader
            material = new pc.Material();
            material.shader = shader;

            // For all meshes in the model
            var model = modelComponent.model;
            model.meshInstances.forEach(function (meshInstance) {
                // Set the new material
                meshInstance.material = material;

                // Set all primitives of the mesh to render as points
                meshInstance.mesh.primitive.forEach(function (primitive) {
                    primitive.type = pc.PRIMITIVE_POINTS;
                })
            })

            currentTime = 0;
            app.on("update", function (dt) {

                // Update the time and pass it to shader
                currentTime += dt;
                material.setParameter('uTime', currentTime);

                // Rotate the model
                entity.rotate(0, 15 * dt, 0);
            });

        });
    </script>
</body>
</html>
